package com.fieldchain.ex.fa100.ui.personal.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.base.MyBaseActivity;
import com.fieldchain.ex.fa100.mvp.presenter.RegistEmailPresenter;
import com.fieldchain.ex.fa100.mvp.view.RegistView;

import com.fieldchain.ex.fa100.utils.common.EditTextUtils;
import com.fieldchain.ex.fa100.utils.common.InputAnimation;
import com.fieldchain.ex.fa100.utils.common.VerticalDisUtils;
import com.fieldchain.ex.fa100.utils.net.common.EmailCode;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/7
 */
public class RegistEmailActivity extends MyBaseActivity<RegistEmailPresenter, RegistView> implements View.OnClickListener, RegistView {
    private RelativeLayout reaBack;
    private ImageView ivBac;
    private EditText etPhone;
    private RelativeLayout reaImgcode;
    private ImageView ivImgcode;
    private EditText etImagecode;
    private EditText etSmsver;
    private TextView tvVerSms;
    private RelativeLayout reaDis;
    private TextView tvSure;
    private LinearLayout linBottom;
    private TextView tvPhone;
    private TextView tvLogin;


    private EditTextUtils etUtils;
    private InputAnimation animation;

    @Override
    public void findView() {
        reaBack = findViewById(R.id.rea_back);
        ivBac = findViewById(R.id.iv_bac);
        etPhone = findViewById(R.id.et_phone);
        reaImgcode = findViewById(R.id.rea_imgcode);
        ivImgcode = findViewById(R.id.iv_imgcode);
        etImagecode = findViewById(R.id.et_imagecode);
        etSmsver = findViewById(R.id.et_smsver);
        tvVerSms = findViewById(R.id.tv_ver_sms);
        reaDis = findViewById(R.id.rea_dis);
        tvSure = findViewById(R.id.tv_sure);
        linBottom = findViewById(R.id.lin_bottom);
        tvPhone = findViewById(R.id.tv_phone);
        tvLogin = findViewById(R.id.tv_login);
    }

    @Override
    public void setValue() {
        etUtils = new EditTextUtils();
        etUtils.append(etSmsver, reaDis);
        etUtils.start();
        setInput();

        reaBack.setOnClickListener(this);
        tvLogin.setOnClickListener(this);
        tvPhone.setOnClickListener(this);
        ivImgcode.setOnClickListener(this);
        tvVerSms.setOnClickListener(this);
        tvSure.setOnClickListener(this);
        presenter.getVer();

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rea_back:
                finish();
                break;
            case R.id.iv_imgcode:
                presenter.getVer();
                break;

            case R.id.tv_ver_sms:
                VerticalDisUtils.getInstance().start(etSmsver, tvVerSms, reaDis);
                EmailCode.getInstance().getEmail(etPhone.getText().toString(), etImagecode.getText().toString());
                break;

            case R.id.tv_phone:
                startActivity(new Intent(mContext, RegistPhoneActivity.class));
                finish();
                break;

            case R.id.tv_login:
                finish();
                break;

            case R.id.tv_sms:
                startActivity(new Intent(mContext, LoginSMSActivity.class));
                break;
            case R.id.tv_sure:
                presenter.regist(etPhone.getText().toString(), etSmsver.getText().toString());
                break;
        }
    }

    public void setInput() {
        animation = new InputAnimation();
        animation.attachView((Activity) mContext, 70, etSmsver, linBottom, ivBac);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        etUtils.cancle();
        animation.detachView();
        animation = null;
        VerticalDisUtils.getInstance().cancle();
    }

    @Override
    public int getLayout() {
        return R.layout.activity_email_regist;
    }


    @Override
    public void registResult(int type) {
        if (type == 200) {
            Intent intent = new Intent(mContext, RegistNextActivity.class);
            intent.putExtra("imagcode", etImagecode.getText().toString());
            intent.putExtra("smsCode", etSmsver.getText().toString());
            intent.putExtra("name", etPhone.getText().toString());
            startActivity(intent);
            finish();
        }else{
            etSmsver.setText("");
            etImagecode.setText("");
            presenter.getVer();
            VerticalDisUtils.getInstance().stop();
        }
    }

    @Override
    public void verImage(Bitmap bit) {
        ivImgcode.setImageBitmap(bit);
    }
}
